package com.excel.analysis;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Node;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * XML处理类
 * 
 * @author Administrator
 * 
 */
public class XMLHandler {

	private Document doc = null;

	/**
	 * 构造函数
	 * 
	 * @param doc
	 * @throws Exception
	 */
	public XMLHandler() throws Exception {
		doc = DocumentHelper.createDocument();
	}

	/**
	 * 构造函数
	 * 
	 * @param doc
	 * @throws Exception
	 */
	public XMLHandler(Document doc) throws Exception {
		this.doc = doc;
	}

	/**
	 * 构造函数 从文件读取
	 * 
	 * @param filePath
	 * @throws Exception
	 */
	public XMLHandler(File filePath) throws Exception {
		SAXReader saxreader = new SAXReader();
		InputStream is = new FileInputStream(filePath);
		doc = saxreader.read(is);
		is.close();
	}

	/**
	 * 构造函数 从文件读取
	 * 
	 * @param filePath
	 * @throws Exception
	 */
	public XMLHandler(InputStream is) throws Exception {
		SAXReader saxreader = new SAXReader();
		doc = saxreader.read(is);
	}

	/**
	 * 构造函数 字符串生成XML
	 * 
	 * @param xml
	 * @throws Exception
	 */
	public XMLHandler(String xml) throws Exception {
		SAXReader saxreader = new SAXReader();
		InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
		doc = saxreader.read(is);
	}

	/**
	 * xml转换
	 * 
	 * @param doc
	 * @param stylesheet
	 * @return
	 * @throws TransformerException
	 */
	public Document xmlTransform(Document doc, InputStream stylesheet)
			throws TransformerException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer(new StreamSource(
				stylesheet));
		DocumentSource ds = new DocumentSource(doc);
		DocumentResult result = new DocumentResult();
		transformer.transform(ds, result);
		Document document = result.getDocument();
		return document;
	}
	
	/**
	 * xml转换html
	 * 
	 * @param doc
	 * @param stylesheet
	 * @return
	 * @throws TransformerException
	 */
	public String xmlTransform(File stylesheet)
			throws TransformerException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
		DocumentSource ds = new DocumentSource(doc);
		DocumentResult result = new DocumentResult();
		transformer.transform(ds, result);
		Document document = result.getDocument();
		return document.asXML();
	}

	/**
	 * xml查询,返回单个Node
	 * 
	 * @param xPath
	 * @throws Exception
	 */
	public Node selectSingleNode(String xPath) throws Exception {
		Node node = doc.selectSingleNode(xPath);
		return node;
	}
	
	/**
	 * 返回xml节点
	 * 
	 * @param xPath
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Node> selectNodes(String xPath) throws Exception {
		List<Node> nodes = doc.selectNodes(xPath);
		return nodes;
	}
	
	/**
	 * 当前元素节点
	 * 
	 * @param xPath
	 * @param e
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Node> selectNodes(String xPath, Element e) throws Exception {
		List<Node> nodes = e.selectNodes(xPath);
		return nodes;
	}

	/**
	 * xml查询,返回单个Node
	 * 
	 * @param xPath
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectNode(String xPath) throws Exception {
		List<Node> nodes = doc.selectNodes(xPath);
		List<String> list = new ArrayList<String>();
		for (Node n : nodes) {
			list.add(n.asXML());
		}
		return list;
	}

	/**
	 * 输出XML
	 * 
	 * @return
	 */
	public String toXml() {
		return doc.asXML();
	}

	/**
	 * 创建XML根
	 * 
	 * @param nodes
	 * @return
	 * @throws Exception
	 */
	public Element createRoot(String rootName) throws Exception {
		return doc.addElement(rootName);
	}

	/**
	 * 获得根节点
	 * 
	 * @return
	 * @throws Exception
	 */
	public Element getRoot() throws Exception {
		return doc.getRootElement();
	}

	/**
	 * 元素最加多个文档
	 * 
	 * @param element
	 * @param nodes
	 */
	public void addNodes(Element element, List<String> nodes) {
		for (String n : nodes) {
			addNode(element, n);
		}
	}

	/**
	 * 元素最加单个文档
	 * 
	 * @param element
	 * @param nodes
	 */
	public void addNode(Element element, String node) {
		Entity entity = DocumentHelper.createEntity("n", node);
		element.add(entity);
	}

	/**
	 * 元素添加子元素
	 * 
	 * @param element
	 * @param name
	 */
	public Element addElement(Element element, String name) {
		return element.addElement(name);
	}

	/**
	 * 
	 * 
	 * @param nodes
	 * @throws Exception
	 */
	public void addNode(List<Node> nodes) throws Exception {

	}

	public Document getDoc() {
		return doc;
	}

	/**
	 * 保存
	 * 
	 * @param file
	 * @throws IOException
	 */
	public void saveAs(File file) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
		writer.write(doc);
		writer.close();
	}

}
